package work04game;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Game {
    private Maze maze;
    private Player player;
    private Scanner scanner;
    private Skill skill1;
    private Skill skill2;
    private Skill skill3;
    private ArrayList<Equipment> equipment = new ArrayList<>();
    private ArrayList<Equipment> wearEquipment = new ArrayList<>();
    public Game() {
        maze = new Maze();
        player = new Player();
        scanner = new Scanner(System.in);
        skill1 = new Skill();
        skill2 = new Skill();
        skill3 = new Skill();
        skill2.GetSkill1();
        skill3.GetSkill2();
    }
    public void start() {
        while (true) {
            maze.printMaze(player);
            System.out.println("生命值: " + player.getHealth());
            System.out.println("输入WASD移动 Q退出: ");
            System.out.println("背包----------按B查看装备/装备装备，按E查看装备栏（最多同时装备2件）/卸下装备");
            PrintEquipment(equipment);

            char command = scanner.next().charAt(0);
            if (command == 'B' || command == 'b') {
                while(true) {
                    System.out.println("选择你要查看的物品(请输入数字),按下r退出背包");
                    int LookCount = scanner.next().charAt(0)-48;
                    if(LookCount < equipment.size()) {
                        printEquipmentNumber(LookCount, equipment);
                        if(scanner.next().charAt(0) == 'y') {
                            if(wearEquipment.size() <2 || equipment.get(LookCount).getName().equals("药瓶")) {
                                wearEquipments(LookCount,equipment,wearEquipment,player);
                                System.out.println("装备成功");
                                for(int i=0;i<wearEquipment.size();i++) {
                                    if(wearEquipment.get(i).getName().equals("药瓶")){
                                        System.out.println("药瓶使用成功");
                                        wearEquipment.remove(i);
                                    }
                                }

                            }else{
                                System.out.println("装备栏位已满！！！");
                            }
                        }
                    }
                if(LookCount + 48=='r'){
                    System.out.println("生命值: " + player.getHealth());
                    System.out.println("输入WASD移动 Q退出: ");
                    System.out.println("背包----------按B查看");
                    System.out.println("装备栏-----------按E查看");
                    break;
                    }
                }
            }
            if (command == 'E' || command == 'e') {
                while(true) {
                    PrintEquipment(wearEquipment);
                    System.out.println("选择你要查看的装备,按下r退出装备栏");
                    int LookCount = scanner.next().charAt(0) - 48;

                    if (LookCount < wearEquipment.size()) {
                        printEquipmentNumber(LookCount, wearEquipment);
                    }
                    else if (LookCount + 48 == 'r') {
                        break;
                    }
                     if (scanner.next().charAt(0) == 'y') {
                        unloadEquipments(LookCount, equipment, wearEquipment, player);
                        System.out.println("装备已卸下");
                    }
                }
            }
            if (command == 'Q' || command == 'q') break;
            movePlayer(command);
            if (player.getHealth() <= 0) {
                System.out.println("游戏失败");
                break;
            }
            if (player.getX() == 7 && player.getY() == 7) {
                System.out.println("恭喜你成功走出迷宫!");
                break;
            }
        }
        scanner.close();
    }
    private void movePlayer(char direction) {
        int newX = player.getX(), newY = player.getY();
        if (direction == 'W' || direction == 'w') newX--;
        else if (direction == 'S' || direction == 's') newX++;
        else if (direction == 'A' || direction == 'a') newY--;
        else if (direction == 'D' || direction == 'd') newY++;

        if (newX < 0 || newX >= 8 || newY < 0 || newY >= 8 || Objects.equals(maze.getCell(newX, newY), "墙")) {
            System.out.println("无法移动!");
        } else {
            if (Objects.equals(maze.getCell(newX, newY), "怪")) {
                battle();
            }
            player.move(newX, newY);
            collectItem(maze.getCell(newX, newY));
                maze.setCell(newX, newY, "空");
        }
    }

    private void collectItem(String cell) {
        switch (cell) {
            case "剑":
                if(equipment.size() < 10) {
                    Equipment equipment1 = new Equipment();
                    equipment1.GetSword();
                    equipment.add(equipment1);
                    break;
                }else{
                    System.out.println("背包已满");
                    break;
                }

            case "盾":
                if(equipment.size() < 10) {
                    Equipment equipment2 = new Equipment();
                    equipment2.GetArmour();
                    equipment.add(equipment2);
                    break;
                }else {
                    System.out.println("背包已满");
                }

            case "药":
                if(equipment.size() < 10) {
                    Equipment equipment3 = new Equipment();
                    equipment3.AddLotion();
                    equipment.add(equipment3);
                    break;
                }else {
                    System.out.println("背包已满");
                }

        }
    }

    private void battle() {
        Monster monster = new Monster();
        System.out.println("遭遇怪物，进入战斗!");
        System.out.println("你的血量："+player.getHealth());
        System.out.println("你的蓝量："+player.getPower());
        System.out.println("怪物血量："+monster.getHealth());
        while (player.getHealth() > 0 && monster.getHealth() > 0) {
            System.out.println("(A)攻击 (S)使用技能");
            char action = scanner.next().charAt(0);
            if (action == 'A' || action == 'a') {
                monster.takeDamage(player.getDamage());
                if (monster.getHealth() > 0) {
                    if(monster.getAttackPower()-player.getDefense()>0){
                        player.Attacked(monster.getAttackPower()-player.getDefense());
                        System.out.println("你受到了"+(monster.getAttackPower()- player.getDefense())+"点伤害");
                    }
                    System.out.println("你造成了"+player.getDamage()+"点伤害");

                    System.out.println("你的血量："+player.getHealth());
                    System.out.println("你的蓝量："+player.getPower());
                    System.out.println("怪物血量："+monster.getHealth());
                }
            } else if (action == 'S' || action == 's') {
                System.out.println("选择你的技能(1/2/3)");
                skill1.printSkill();skill2.printSkill();skill3.printSkill();
                int SId = scanner.next().charAt(0)-48;
                if (SId == 1) {
                    player.setPower(player.getPower() - skill1.getPower());
                    monster.takeDamage(skill1.getDamage()+ player.getDamage());
                    if (monster.getHealth() > 0) {
                        if(monster.getAttackPower()-player.getDefense()>0){
                            player.Attacked(monster.getAttackPower()-player.getDefense());
                            System.out.println("你受到了"+(monster.getAttackPower()- player.getDefense())+"点伤害");
                        }
                        System.out.println("你造成了"+(skill1.getDamage()+ player.getDamage())+"点伤害");
                        System.out.println("你的血量："+player.getHealth());
                        System.out.println("你的蓝量："+player.getPower());
                        System.out.println("怪物血量："+monster.getHealth());
                    }else{
                        System.out.println("得胜");
                    }
                }
                else if (SId == 2) {
                    player.setPower(player.getPower() - skill2.getPower());
                    monster.takeDamage(skill2.getDamage()+ player.getDamage());
                    if (monster.getHealth() > 0) {
                        if(monster.getAttackPower()-player.getDefense()>0){
                            player.Attacked(monster.getAttackPower()-player.getDefense());
                            System.out.println("你受到了"+(monster.getAttackPower()- player.getDefense())+"点伤害");
                        }
                        System.out.println("你造成了"+(skill2.getDamage()+ player.getDamage())+"点伤害");
                        System.out.println("你的血量："+player.getHealth());
                        System.out.println("你的蓝量："+player.getPower());
                        System.out.println("怪物血量："+monster.getHealth());
                    }else {
                        System.out.println("得胜");
                    }
                }
                else if (SId == 3) {
                    player.setPower(player.getPower() - skill3.getPower());
                    player.setHealth(Math.min(player.getHealth() + skill3.getDamage(), 100));
                    monster.takeDamage(skill2.getDamage()+ player.getDamage());
                    if (player.getHealth() > 0) {
                        if(monster.getAttackPower()-player.getDefense()>0){
                            player.Attacked(monster.getAttackPower()-player.getDefense());
                            System.out.println("你受到了"+(monster.getAttackPower()- player.getDefense())+"点伤害");
                        }
                        System.out.println("你治疗了"+skill3.getDamage()+"点生命");
                        System.out.println("你的血量："+player.getHealth());
                        System.out.println("你的蓝量："+player.getPower());
                        System.out.println("怪物血量："+monster.getHealth());
                    }else {
                        System.out.println("得胜");
                    }
                }
            }
        }
    }
    public void PrintEquipment(ArrayList<Equipment> equipment) {
        for (int i = 0; i < equipment.size(); i++) {
            System.out.print( i+ ":" + equipment.get(i).getName()+ " ");
        }
    }
    public void printEquipmentNumber(int number, ArrayList<Equipment> equipment) {
        System.out.println("名称："+equipment.get(number).getName());
        System.out.println("攻击加成："+equipment.get(number).getATK());
        System.out.println("防御加成："+equipment.get(number).getDEF());
        System.out.println("生命回复："+equipment.get(number).getHP());
        System.out.println("法力回复："+equipment.get(number).getMP());
        System.out.println("按下Y使用/装备/卸下,按下r返回");
    }

    public void wearEquipments(int number, ArrayList<Equipment> equipment,ArrayList<Equipment> wearEquipment,Player player) {
        player.setHealth(Math.min(player.getHealth() + equipment.get(number).getHP(), 100));
        player.setPower(Math.min(player.getPower() + equipment.get(number).getMP(), 50));
        player.setPower(player.getPower() + equipment.get(number).getMP());
        player.setDamage(player.getDamage() + equipment.get(number).getATK());
        player.setDefense(player.getDefense() + equipment.get(number).getDEF());
        wearEquipment.add(equipment.get(number));
        equipment.remove(number);
    }
    public void unloadEquipments(int number, ArrayList<Equipment> equipment,ArrayList<Equipment> wearEquipment,Player player){
        player.setHealth(player.getHealth() - wearEquipment.get(number).getHP());
        player.setPower(player.getPower() - wearEquipment.get(number).getMP());
        player.setDamage(player.getDamage() - wearEquipment.get(number).getATK());
        player.setDefense(player.getDefense() - wearEquipment.get(number).getDEF());
        equipment.add(wearEquipment.get(number));
        wearEquipment.remove(number);
    }
}
